//package 并发编程19.死锁;


/**
 * @author idea
 * @data 2019/2/24
 */
public class DeadLockDemo extends Thread{

    private String first;

    private String second;

    public DeadLockDemo(String name,String first,String second){
        super(name);
        this.first=first;
        this.second=second;
    }

    @Override
    public void run(){
     synchronized (first){
         System.out.println(this.getName()+" obtained:"+first);
         try {
             Thread.sleep(1000L);
             synchronized (second){
                 System.out.println(Thread.currentThread().getName()+"this is 2");
             }
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
    }

    public static void main(String[] args) throws InterruptedException {
        String lockA="lockA";
        String lockB="lockB";
        DeadLockDemo deadLock=new DeadLockDemo("t1",lockA,lockB);
        DeadLockDemo deadLock2=new DeadLockDemo("t2",lockB,lockA);
        deadLock.setPriority(2);
        deadLock2.setPriority(1);
        deadLock.start();
        deadLock2.start();
//        deadLock.join();
//        deadLock2.join();
    }
}
